package com.shengzhe.ostudent.controller.admin;

import com.shengzhe.ostudent.common.exception.OsException;
import com.shengzhe.ostudent.controller.base.BaseController;
import com.shengzhe.ostudent.entity.SysUser;

import com.shengzhe.ostudent.entity.SysUserCondition;
import com.shengzhe.ostudent.service.sysuser.SysUserService;
import com.shengzhe.ostudent.service.sysuser.SysUserServiceImpl;
import com.shengzhe.ostudent.utils.result.E;
import com.shengzhe.ostudent.utils.result.R;
import com.shengzhe.ostudent.utils.security.JwtUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/admin/sysuser/*")
public class AdminSysUserController extends BaseController {
    private final SysUserService sysUserService = new SysUserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pathInfo = super.getPathInfo();
        R r = R.ok();
        switch (pathInfo) {
            case "/list":
                System.out.println("AdminSysUserController>>>doGet>>list");
                SysUserCondition sysUserCondition = super.getReqBean(SysUserCondition.class);
                r.setEntityList(sysUserService.list(sysUserCondition));
                r.writeJson(resp);
                break;
            case "/testToken":
                System.out.println("AdminSysUserController>>>doGet>>testtoken");
//                String token = req.getHeader("token");
//                JwtUtils.checkToken(token);
                System.out.println("testToken successful");
                r.writeJson(resp);
            default:
                System.out.println("AdminSysUserController>>>doGet>>default");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pathInfo = super.getPathInfo();
        R r = R.ok();
        switch (pathInfo) {
            case "/register":
                System.out.println("AdminSysUserController>>>doPost>>register");
                SysUser sysUser = super.getBodyBean(SysUser.class);
                sysUserService.register(sysUser);
                r.writeJson(resp);
                break;
            case "/login":
                SysUser sysUserLogin = super.getBodyBean(SysUser.class);
//                r.ifSuccess(sysUserService.login(sysUserLogin));
                String token = sysUserService.login(sysUserLogin);
                Map<String, String> map = new HashMap<String,String>(){{put("token", token);}};
//                Map<String, Object> map = new HashMap<>();
//                map.put("token", token);
//                map.put("sysuser", sysUserLogin);
                r.setEntity(map);
                r.writeJson(resp);

                break;
            default:
                System.out.println("AdminSysUserController>>>doPost>>default");
        }
    }
}
